home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / dev / gcc / ixemul_src.lha / ixemul-41.0 / stack / stkext.c < prev    next >
C/C++ Source or Header  |  1995-05-23  |  3KB  |  134 lines

  1. #include <stabs.h>
  2. #include <bases.h>
  3.  
  4. asm("
  5.     .text
  6.     .even
  7.     .globl    ___stkext_f
  8.     .globl    ___stkext
  9.     .globl    ___exit_stk
  10.     .globl    ___stk_free
  11.  
  12. ___stkext_f:
  13.     movel    a0,sp@-
  14.     movel    sp,a0            | memorize sp
  15.     moveml    #0xc062,sp@-        | preserves all registers
  16.     movel    sp,a2            | memorize sp
  17.     addl    #28:W,d1        | Used by this function itself
  18.     movel    "A4(___stackborders)",a1| get upper border of stack in a1
  19.     movel    a1@(4:W),a1
  20.     tstl    "A4(___used_stack)"
  21.     bne    l0
  22.     movel    "A4(___SaveSP)",a1
  23. l0:    jbsr    ___stkext        | change to a stackframe with d0 bytes
  24.     movel    "A4(___used_stack)",a6    | fix stored sp
  25.     addl    d1,a6@(12:W)
  26.     movel    a2,d0            | calculate number of bytes to copy
  27.     addl    "A4(___stk_argbytes)",d0| to new stackframe in d0
  28.     addl    d1,d0
  29.     cmpl    a1,d0            | never copy over the bottom of the stack
  30.     jls    l1
  31.     movel    a1,d0
  32. l1:    subl    a0,d0            | copy
  33.     subl    d0,sp
  34.     movel    sp,a1
  35.     movel    "A4(_SysBase)",a6
  36.     jsr    a6@(-0x270)        | CopyMem(src:a0,dst:a1,siz:d0)
  37.     movel    a2,a0
  38.     moveml    a0@+,#0x4603
  39.     movel    sp,a0            | Prepare cleanup
  40.     addl    d1,a0
  41.     movel    #___stkrst_f,a0@(8:W)
  42.     movel    sp@+,a0
  43.     rts
  44.  
  45. ___stkext:
  46.     movel    a0,sp@-
  47.     moveml    #0xf072,sp@-        | preserves all registers
  48.     movel    "A4(___stk_safezone)",d3
  49.     lea    "A4(___stk_limit)",a2
  50.     lea    "A4(___used_stack)",a3
  51.     addqw    #4,a3
  52.     movel    "A4(_SysBase)",a6
  53.     movel    d0,d2
  54.     addl    d3,d2
  55.     addl    "A4(___stk_argbytes)",d2| required stack
  56. l3:    movel    a3@,a0            | Look for a spare stackframe
  57.     movel    a0,d0
  58.     jne    l6
  59.     addl    #20:W,d2        | no more stackframes - allocate a new one
  60.     movel    "A4(___stk_minframe)",d1
  61.     cmpl    d1,d2
  62.     jge    l4
  63.     movel    d1,d2
  64. l4:    movel    d2,d0
  65.     moveql    #1,d1            | MEMF_PUBLIC
  66.     jsr    a6@(-0xc6)        | AllocMem(siz:d0,typ:d1)
  67.     tstl    d0
  68.     jeq    ___stkovf        | allocation failed
  69. l5:    movel    d0,a0            | prepare new frame for use
  70.     addl    d0,d2
  71.     movel    d2,a0@(8:W)
  72.     addl    #20:w,d0
  73.     movel    d0,a0@(4:W)
  74.     jra    l7
  75. l6:    movel    a0@,a3@            | Remove spare stackframe from list
  76.     movel    a0@(8:W),d0        | Test spare stackframe
  77.     subl    a0@(4:W),d0
  78.     cmpl    d0,d2
  79.     jls    l7
  80.     addl    #20:W,d0        | Not big enough
  81.     movel    a0,a1
  82.     jsr    a6@(-0xd2)        | FreeMem(mem:a1,siz:d0)
  83.     jra    l3
  84. l7:    movel    a3@-,a0@        | Add it to the used list
  85.     movel    a0,a3@
  86.     addqw    #4,a0
  87.     movel    a2@,a0@(12:W)
  88.     movel    a0@,a2@            | Prepare ___top_of_stackframe variable
  89.     addl    d3,a2@
  90.     lea    a0@(4:W),a2
  91.     movel    a2@+,a2@        | Prepare target sp
  92.     jsr    a6@(-0x2dc)        | StackSwap(sss:a0)
  93.     movel    a2@,a0
  94.     addl    #40:W,a2@        | Fix stored sp
  95.     moveml    a0@+,#0x4e0f
  96.     movel    a0@(4:W),sp@-        | prepare returnaddress
  97.     movel    a0@,a0
  98.     rts
  99.  
  100. ___exit_stk:
  101.     tstl    "A4(___used_stack)"
  102.     jeq    ___stk_free
  103.     lea    sp@(12:W),a0
  104.     movel    "A4(___SaveSP)",d0    | Go back to first stackframe
  105.     jbsr    ___stkrst
  106.     movel    a0@,sp@-        | restore argument of exit()
  107.     movel    a0@-,sp@-        | returnaddress for exit()
  108.     movel    a0@-,sp@-        | returnaddress for callfuncs()
  109.     movel    a0@-,sp@-        | our returnaddress
  110. |    jbsr    ___stk_free
  111. |    rts
  112.  
  113. ___stk_free:
  114.     movel    a2,sp@-
  115.     movel    a6,sp@-            | Free all unused stackframes
  116.     movel    "A4(_SysBase)",a6
  117.     lea    "A4(___used_stack)",a2
  118.     addqw    #4,a2
  119. l8:    movel    a2@,a1            | Remove spare stackframe from list
  120.     movel    a1,d0
  121.     jeq    l9
  122.     movel    a1@,a2@
  123.     movel    a1@(8:W),d0        | Calculate size
  124.     subl    a1@(4:W),d0
  125.     addl    #20:W,d0
  126.     jsr    a6@(-0xd2)        | FreeMem(mem:a1,siz:d0)
  127.     jra    l8
  128. l9:    movel    sp@+,a6
  129.     movel    sp@+,a2
  130.     rts
  131. ");
  132.  
  133. ADD2EXIT(__exit_stk,-50);
  134.